﻿var viewStatus = this.lookupController().analysis().status;
var serviceType = formData.agencyInfo.serviceType;
var vm = this.lookupViewModel();
var workLocationValue = ref.workLocation.getValue();
var hasNewBundOffice = workLocationValue.shanghai_New_Bund_Office == 1;
var hasRDC = workLocationValue.rdc_JLR_Office == 1 || workLocationValue.rdc_Warehouse_W == 1;
var hasJingqiao = workLocationValue.shanghai_jinqiao == 1;
var hasOther = workLocationValue.pdc_Suzhou_W == 1
    || workLocationValue.beijing_Office == 1
    || workLocationValue.pdc_Foshan_W == 1
    || workLocationValue.beijing_Academy == 1
    || workLocationValue.pdc_Beijing_W == 1
    || workLocationValue.guangzhou_Office == 1
    || workLocationValue.pdc_Chongqing_W == 1
    || workLocationValue.guangzhou_Academy == 1
    || workLocationValue.cjlr_Hongqiao_W == 1
    || workLocationValue.chengdu_Office == 1
    || workLocationValue.cjlr_Changshu_W == 1;

var hasPDC = workLocationValue.pdc_Suzhou_W == 1
    || workLocationValue.pdc_Foshan_W == 1
    || workLocationValue.pdc_Beijing_W == 1
    || workLocationValue.pdc_Chongqing_W == 1
    || workLocationValue.cjlr_Hongqiao_W == 1
    || workLocationValue.cjlr_Changshu_W == 1

var hasPdcOther = workLocationValue.shanghai_New_Bund_Office == 1
    || workLocationValue.rdc_Warehouse_W == 1
    || workLocationValue.shanghai_jinqiao == 1
    || workLocationValue.beijing_Office == 1
    || workLocationValue.beijing_Academy == 1
    || workLocationValue.guangzhou_Office == 1
    || workLocationValue.guangzhou_Academy == 1
    || workLocationValue.chengdu_Office == 1
    || workLocationValue.rdc_JLR_Office == 1

var companyResourcesValue = ref.companyResources.getValue();
var email = ref.email.items.items;

//不可以申请门禁卡之后，Access Card Start Date和Access Card End Date均应该灰掉
var hasAccessCard = companyResourcesValue.accessCard == 1;

ref.cardStartDate.setAllowBlank(!hasAccessCard);
ref.cardEndDate.setAllowBlank(!hasAccessCard);

//除JV Partner以外，其他服务类型的人员没有勾选CDSID时，不能申请Skype Account、JLR Family、Email
if (serviceType != '4') {
    if (companyResourcesValue.cdsid == 0) {
        Ext.each(email, function (ctr) {
            ctr.setDisabled(ctr.inputValue != 'Neither');
        });
    }
}
if (viewStatus == 'launch' || viewStatus == 'resubmit') {
    var companyResourcesItems = ref.companyResources.items.items;

    var cardStartDate = ref.cardStartDate.getValue();
    var serviceStart = ref.serviceStart.getValue();

    ref.cardStartDate.setDisabled(!hasAccessCard);
    ref.cardEndDate.setDisabled(!hasAccessCard);
    if (self.containsFocus) {
        ref.cardStartDate.setValue(null);
        ref.cardEndDate.setValue(null);
    };

    //楼层控制.
    if (self.containsFocus) {
        if (hasAccessCard == true && self.containsFocus) {
            ref.newBundFloors.setAllowBlank(!hasNewBundOffice);
            ref.newBundFloors.setDisabled(!hasNewBundOffice);
        } else {
            ref.newBundFloors.setAllowBlank(true);
            ref.newBundFloors.setDisabled(true);
        }
    }

    // ref.newBundFloors.setAllowBlank(!hasNewBundOffice);
    // ref.newBundFloors.setDisabled(!hasNewBundOffice);
    if (!hasNewBundOffice) {
        Ext.each(ref.newBundFloors.items.items, function (ctr) {
            ctr.setValue(false);
        });
    } else {
        var newBundFloorsArr = vm.get('NebulogyForm.originalNewBundFloors') || [];
        Ext.each(ref.newBundFloors.items.items, function (ctr) {
            if (ctr.inputValue == 'facialRecognition') {
                ctr.setValue(false);
                ctr.setDisabled(true);
                //如果包含NewBundOffice并且 Service Type = 2(Long-term Outsourcing Service) 和 4 (JV Partner),Facial Recognition才可以选中.
                if (hasNewBundOffice && (serviceType == 2 || serviceType == 4)) {
                    ctr.setDisabled(false);
                }
            }

            //默认赋值原始的newBoundFloors 数据
            if (Ext.Array.contains(newBundFloorsArr, ctr.boxLabel)) {
                ctr.setValue(true);
            }
        });
    }

    var fieldNameList = ['prs', 'prsAmounts', 'pos', 'posAmounts'];

    Ext.each(fieldNameList, function (field) {
        ref[field].setDisabled(false);
        ref[field].setAllowBlank(false);
    });

    ref.email.setValue("");

    switch (serviceType) {
        case '1':
            //companyResources 可选项控制
            Ext.each(companyResourcesItems, function (ctr) {
                ctr.setDisabled(false);
                ctr.setValue(false);
                if (ctr.inputValue != 'cdsid' && ctr.inputValue != 'accessCard' && ctr.inputValue != 'none_') {
                    ctr.setDisabled(true);
                }
                if (hasPDC && !hasPdcOther) {
                    if (ctr.inputValue == 'accessCard') {
                        ctr.setDisabled(true);
                    }
                }
            });

            Ext.each(email, function (ctr) {
                ctr.setDisabled(ctr.inputValue != 'Neither');
            });

            if (cardStartDate && hasAccessCard) {
                //ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
                ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
            }
            break;
        case '2':
            //companyResources 可选项控制
            Ext.each(companyResourcesItems, function (ctr) {
                ctr.setDisabled(false);
                ctr.setValue(false);
                switch (ctr.inputValue) {
                    case 'accessCard':
                        if (hasPDC && !hasPdcOther) {
                            ctr.setDisabled(true);
                        }
                        break;
                    case 'skypeAccount':
                        if (hasOther && (!hasNewBundOffice && !hasRDC && !hasJingqiao)) {
                            ctr.setDisabled(true);
                        }
                        break;
                    case 'locker':
                        if ((hasRDC || hasJingqiao || hasOther) && !hasNewBundOffice) {
                            ctr.setDisabled(true);
                        }
                        break;
                    default:
                }
            });

            //email 控制
            Ext.each(email, function (ctr) {
                ctr.setDisabled(false);

                if (ctr.inputValue == 'IMSS' && hasRDC && !hasNewBundOffice && !hasJingqiao && !hasOther) {
                    ctr.setDisabled(true);
                }
            });

            if (cardStartDate && hasAccessCard) {
                //ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
                ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
            }
            break;
        case '3':
            //companyResources 可选项控制
            Ext.each(companyResourcesItems, function (ctr) {
                ctr.setDisabled(false);
                ctr.setValue(false);
                switch (ctr.inputValue) {
                    case 'accessCard':
                        if (hasPDC && !hasPdcOther) {
                            ctr.setDisabled(true);
                        }
                        break;
                    case 'skypeAccount':
                        if (hasOther && (!hasNewBundOffice && !hasRDC && !hasJingqiao)) {
                            ctr.setDisabled(true);
                        }
                        break;
                    case 'locker':
                        if ((hasRDC || hasJingqiao || hasOther) && !hasNewBundOffice) {
                            ctr.setDisabled(true);
                        }
                        break;
                    default:
                }
            });

            //email 控制
            Ext.each(email, function (ctr) {
                ctr.setDisabled(false);

                if (ctr.inputValue == 'IMSS' && hasRDC && !hasNewBundOffice && !hasJingqiao && !hasOther) {
                    ctr.setDisabled(true);
                }
            });

            if (cardStartDate && hasAccessCard) {
                //ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
                ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.MONTH, 6));
            }
            break;
        case '4':
            Ext.each(fieldNameList, function (field) {
                ref[field].setValue('');
                ref[field].setDisabled(true);
                ref[field].setAllowBlank(true);
            });

            //companyResources 可选项控制
            Ext.each(companyResourcesItems, function (ctr) {
                ctr.setDisabled(false);
                ctr.setValue(false);

                if (ctr.inputValue == 'cdsid') {
                    ctr.setDisabled(true);
                } else {
                    switch (ctr.inputValue) {
                        case 'accessCard':
                            if (hasPDC && !hasPdcOther) {
                                ctr.setDisabled(true);
                            }
                            break;
                        case 'skypeAccount':
                            if ((hasRDC || hasOther) && (!hasNewBundOffice && !hasJingqiao)) {
                                ctr.setDisabled(true);
                            }
                            break;
                        case 'jlrFamily':
                        case 'accessCard':
                            if (hasRDC && (!hasNewBundOffice && !hasJingqiao && !hasOther)) {
                                ctr.setDisabled(true);
                            }
                            break;
                        case 'locker':
                            if ((hasRDC || hasJingqiao || hasOther) && !hasNewBundOffice) {
                                ctr.setDisabled(true);
                            }
                            break;
                        default:
                    }
                }
            });

            //email 控制
            if (hasRDC && !hasNewBundOffice && !hasJingqiao && !hasOther) {
                Ext.each(email, function (ctr) {
                    ctr.setDisabled(ctr.inputValue != 'Neither');
                });
                ref.email.setValue('');
            } else {
                Ext.each(email, function (ctr) {
                    ctr.setDisabled(false);
                    if (ctr.inputValue == 'JLR') {
                        ctr.setDisabled(true);
                    }
                });
            }

            //门禁卡验证.
            if (!hasRDC && cardStartDate && hasAccessCard) {
                //ref.cardEndDate.setValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
                ref.cardEndDate.setMaxValue(Ext.Date.add(cardStartDate, Ext.Date.YEAR, 1));
            }
            break;
    }
}

var jlrOffice = [],
    wareHouse = [],
    roleList = [];

Ext.each(ref.workLocation.items.items, function (ctr) {
    if (ctr.checked) {
        if (ctr.inputValue.indexOf('_W') == -1) {
            jlrOffice.push(ctr.boxLabel);
        } else {
            wareHouse.push(ctr.boxLabel);
        }

        switch (ctr.inputValue) {
            case 'shanghai_New_Bund_Office':
                roleList.push('NBAccessCard');
                break;
            case 'shanghai_jinqiao':
                roleList.push('JQAccessCard');
                break;
            case 'rdc_Warehouse_W':
            case 'rdc_JLR_Office':
                roleList.push('RDCAccessCard');
                break;
            case 'beijing_Office':
            case 'beijing_Academy':
                roleList.push('BJAccessCard');
                break;
            case 'chengdu_Office':
                roleList.push('CDAccessCard');
                break;
            case 'guangzhou_Office':
            case 'guangzhou_Academy':
                roleList.push('GZAccessCard');
                break;

        }
    }
});

vm.set('NebulogyForm.roleName', '"' + Array.from(new Set(roleList)).join('","') + '"');
vm.set('NebulogyForm.jlrOfficeStr', jlrOffice.join(','));
vm.set('NebulogyForm.wareHouseStr', wareHouse.join(','));

//勾选的值还需要同步到手机端的 jlrOffice 和warehouse&CJLR
//获取勾选的boxLabel
Ext.each(ref.jlrOffice.items.items, function (ctr) {
    ctr.setValue(newValue[ctr.inputValue] == 1 ? true : false);
});
Ext.each(ref.warehouseCJLR.items.items, function (ctr) {
    ctr.setValue(newValue[ctr.inputValue] == 1 ? true : false);
});

vm.set('NebulogyForm.agencyInfo.jlrOffice', ref.jlrOffice.getValue());
vm.set('NebulogyForm.agencyInfo.warehouseCJLR', ref.warehouseCJLR.getValue());
